<?php

namespace Action\User\Picture;

class ByUrl extends \Library\Action\UserAction
	implements \Library\Action\AjaxAction {
	
	private $_imagesMimes = array(
		'image/jpeg',
		'image/png',
		'image/gif'
	);
	private $_picture;

	public function execute() {
		parent::execute();
		
		$this->setView( 'Action/User/PictureDialog/SizePanel' );
		
		$url = $this->getParam( 'url' );
		
		_c( 'Url', $url );
		_a( $this->_checkImage( $url ), 'You must give an image url' );

		$content = file_get_contents( $url );
		
		$this->_picture = new \Library\Picture( $content );
	}
	
	private function _checkImage( $url ) {
		foreach( get_headers( $url ) as $header ) {
			if( preg_match( '/^Content-Type\s*:\s*(\S+)/is', $header, $match ) ) {
				return in_array( mb_strtolower($match[1]), $this->_imagesMimes );
			}
		}
		return false;
	}
	
	public function getPicture() {
		return $this->_picture;
	}
	
}
